package com.chc.wxapi.answer_question.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chc.wxapi.answer_paper.entity.AnswerPaper;
import com.chc.wxapi.answer_paper.service.AnswerPaperService;
import com.chc.wxapi.answer_paper_choice.entity.AnswerPaperChoice;
import com.chc.wxapi.answer_paper_choice.service.AnswerPaperChoiceService;
import com.chc.wxapi.answer_question.entity.AnswerQuestion;
import com.chc.wxapi.answer_question.entity.CommitParm;
import com.chc.wxapi.answer_question.entity.PaperPrm;
import com.chc.wxapi.answer_question.mapper.AnswerQuestionMapper;
import com.chc.wxapi.answer_question.service.AnswerQuestionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 *
 * @Author: chc
 * @Date: 2022/04/01/15:13
 * @Description:
 */
@Service
public class AnswerQuestionServiceImpl extends ServiceImpl<AnswerQuestionMapper, AnswerQuestion> implements AnswerQuestionService {

    //试题选项service层
    @Autowired
    private AnswerPaperChoiceService answerPaperChoiceService;

    //试题service层
    @Autowired
    private AnswerPaperService answerPaperService;

    @Override
    @Transactional //事务回滚
    public void saveCommit(CommitParm parm) {
        //保存answer_question
        AnswerQuestion answerQuestion = new AnswerQuestion();
        answerQuestion.setOpenId(parm.getOpenid());
        answerQuestion.setQuestionId(parm.getQuestionId());
        answerQuestion.setCreateTime(new Date());
        this.baseMapper.insert(answerQuestion);
        //保存answer_paper表
        //前端传来的试题
        List<PaperPrm> paperList = parm.getPaperList();
        List<AnswerPaper> list = new ArrayList<>();
        for (int i = 0; i < paperList.size(); i++) {
            AnswerPaper answerPaper = new AnswerPaper();
            BeanUtils.copyProperties(paperList.get(i),answerPaper);
            answerPaper.setOpenid(parm.getOpenid());
            list.add(answerPaper);
            //保存answer_paper_choice表
            //单选和多选需要分开单独保存选项'
            //单选
            if (paperList.get(i).getPaperType().equals("1")){
                AnswerPaperChoice choice = new AnswerPaperChoice();
                choice.setOpenId(parm.getOpenid());
                choice.setPaperId(paperList.get(i).getPaperId());
                choice.setChoiceId(Long.valueOf(paperList.get(i).getPaperValue()));
                //把单选项保存到数据库表中
                answerPaperChoiceService.save(choice);
            }
            //多选
            if (paperList.get(i).getPaperType().equals("2")){
                String rex = ",";
                String[] strings = paperList.get(i).getPaperValue().split(rex);
                List<AnswerPaperChoice> choiceList = new ArrayList<>();
                for (int j = 0; j < strings.length; j++) {
                    AnswerPaperChoice choice = new AnswerPaperChoice();
                    choice.setOpenId(parm.getOpenid());
                    choice.setPaperId(paperList.get(i).getPaperId());
                    choice.setChoiceId(Long.valueOf(strings[j]));
                    choiceList.add(choice);
                }
                //把多选项保存到数据库表中
                answerPaperChoiceService.saveBatch(choiceList);
            }
        }

        answerPaperService.saveBatch(list);

    }
}
